package org.biubiu0002.baiblog.core.localcache;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
 * @Author: biubiu0002
 * @Date: 2022-05-17 0:12
 **/

public abstract class BaseCacheService<K, V> {
    private LoadingCache<K, V> cache;

    protected BaseCacheService() {
        cache = CacheBuilder.newBuilder()
                .maximumSize(10000)
                .build(new CacheLoader<K, V>() {

                    @Override
                    public V load(K k) throws Exception {
                        return loadData(k);
                    }
                });
    }

    /**
     * 加载数据
     */
    public abstract V loadData(K k);

    /**
     * 读取缓存
     */
    public V getCache(K param){
        return cache.getUnchecked(param);
    }

    /**
     * 更新缓存
     */
    public void refresh(K k){
        cache.refresh(k);
    }





}
